/*
 * ReturnHandler.java
 *
 * Created on March 28, 2007, 11:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.group1E;

import edu.byu.isys413.group1E.data.*;
import java.sql.SQLException;

/**
 *
 * @author Kendall Frazier
 */
public class ReturnHandler {

    
    private static ReturnHandler instance = null;
    
    /** Creates a new instance of SaleHandler */
    public ReturnHandler() {
    }
    
    /**
     *This gets an instance of return handler 
     *
     * @return instance a returnHandler 
     */
    public static synchronized ReturnHandler getInstance() {
        if (instance == null) {
            instance = new ReturnHandler();
        }
        return instance;
    }
    
    public TransactionBO lookupOldTransaction(String txID) throws Exception{
        TransactionDAO txDAO = TransactionDAO.getInstance();
        TransactionBO txBO = txDAO.read(txID);
        return txBO;
    }
    
    /**
     * This class makes a return sales Revenue Source.  this revenue source has a negative price. 
     *
     * @param oldSKU sku of the returned item
     * @param qtyToReturn 
     * @throws java.lang.Exception 
     * @return 
     */
    public SaleBO MakeReturnRevenueSource(String oldSKU, int qtyToReturn)throws Exception{
        SaleDAO saleDAO = SaleDAO.getInstance();
        SaleBO saleBO = null;
        try{            
            saleBO = (SaleBO)saleDAO.create();
        }catch(Exception e){
            e.printStackTrace();
        }
        
        char first = oldSKU.charAt(0);
        if(first == 'P'){
            ForSaleDAO forSaleDAO = ForSaleDAO.getInstance();
            ForSaleBO forSaleBO = (ForSaleBO)forSaleDAO.readBySKU(oldSKU);
            
            saleBO.setProduct(forSaleBO);                         
            
        }//end forSale if
        if(first == 'C'){
            ConceptualDAO conceptualDAO = ConceptualDAO.getInstance();
            ConceptualBO conceptualBO = (ConceptualBO)conceptualDAO.readBySKU(oldSKU);
            
            saleBO.setProduct(conceptualBO);            
            
        }//end conceptual if
        
        saleBO.setReturnLine(true);
        saleBO.setDescription(saleBO.getProduct().getProdType());
        saleBO.setPrice(-saleBO.getProduct().getPrice());
        saleBO.setQuantity(qtyToReturn);
        
        return saleBO;
    }//end createTransactionWithReturnLine
       
}
